const app = getApp()
Page({
  data: {
		disabled:false,
		editCoupon:false,
		couponData:{},
    coupon:{
			types:0,
			howmuch:0,
			discount:8,
			amount:0,
			price:0,
			name:'',
			tips:''
		},
	},
	//生命周期函数--监听页面加载
	onLoad: function (options){
		app.getParam(options,(param) => {
			this.data.id = app.util().isNull(param.id) ? 0 : param.id
			if(this.data.id){
				this.getCoupon()
			}
		})
	},
	//读取优惠券
	getCoupon: function () {
    app.api().Get('api/v1/smartbc/coupon/storeCouponInfo',{id:this.data.id},(result) => {
      this.setData({
				coupon:result.data.coupon,
				couponData:result.data,
				editCoupon:true,
      });
    })
	},
	//删除优惠券
	onDelete: function () {
    app.api().Get('api/v1/smartbc/coupon/storeCouponDel',{id:this.data.id},(result) => {
			app.wxAlert(result.msg,()=> {
				wx.navigateBack({
					delta: 1
				})
			})   
    })
	},
	/**
	 * 创建会员卡
	 */
  formSubmit: function (e) {
		var data = e.detail.value;
		switch (this.data.coupon.types) {
			case 1:
				if (app.util().isNull(data.discount) || data.discount <= 0) {
					app.wxAlert('折扣力度必须填写');
					return;
				}
				break;
			case 2:
				if (app.util().isNull(data.amount) || data.amount <= 0) {
					app.wxAlert('储值额度必须填写');
					return;
				}
				break;
			default:
				if (app.util().isNull(data.price) || data.price <= 0) {
					app.wxAlert('减免金额必须填写');
					return;
				}
				if (app.util().isNull(data.howmuch) || data.howmuch < data.price) {
					app.wxAlert('使用门槛不能小于减免金额');
					return;
				}
				break;
		}
    if (app.util().isNull(data.name)) {
			app.wxAlert('优惠券名称必须填写');
    }else if (app.util().isNull(data.howmuch)) {
			app.wxAlert('使用门槛必须填写');
    } else if (app.util().isNull(data.tips)) {
			app.wxAlert('规则简述必须填写');
    } else {
			this.setData({
				disabled:true
			})
			setTimeout(()=>{
				this.setData({
					disabled:false
				})
			},2000)
      var parms = {
				id:this.data.id,
        types:this.data.coupon.types,
        discount: app.util().isNull(data.discount) ? 0 : data.discount,
        price: app.util().isNull(data.price) ? 0 : data.price,
        amount: app.util().isNull(data.amount) ? 0 : data.amount,
        howmuch: app.util().isNull(data.howmuch) ? 0 : data.howmuch,
        name: data.name,
        tips: data.tips,
      }
      app.api().Post('api/v1/smartbc/coupon/edit',parms,function(rel){
        app.wxAlert(rel.msg,()=> {
					wx.navigateBack({
						delta: 1
					})
				})
      })
  	}
	},
	/**
	 * 切换优惠券类型
	 */
	onClickTabs: function (e) {
		var coupon = this.data.coupon;
				coupon.types = e.detail.index;
		this.setData({
			coupon:coupon
		})
	},
	onDiscount: function (e) {
		var coupon = this.data.coupon;
				coupon.discount = e.detail;
		this.setData({
			coupon:coupon
		})
	},
	onPrice: function (e) {
		var coupon = this.data.coupon;
				coupon.price = app.util().isNull(e.detail)?0:e.detail;
		this.setData({
			coupon: coupon
		})
	},
	onAmount: function (e) {
		var coupon = this.data.coupon;
		coupon.amount = app.util().isNull(e.detail) ? 0 :e.detail;
		this.setData({
			coupon:coupon
		})
	},
	onHowmuch: function (e) {
		var coupon = this.data.coupon;
		coupon.howmuch = app.util().isNull(e.detail) ? 0 :e.detail;
		this.setData({
			coupon:coupon
		})
	},
	onName: function (e) {
		var coupon = this.data.coupon;
		coupon.name = app.util().isNull(e.detail) ? '' :e.detail;
		this.setData({
			coupon:coupon
		})
	},
	onTips: function (e) {
		var coupon = this.data.coupon;
		coupon.tips = app.util().isNull(e.detail) ? '' :e.detail;
		this.setData({
			coupon:coupon
		})	
	},
})